using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneChatTyping
	{
		public class TypingStatusContext
		{
			public byte byte_0;
			public byte flag;
			public int int_0;
			public string receivers;
			public byte[] content;
		}
		public class TypingStatusArgs
		{
			public string fromUser;
			public ChatTypingStatus status;
		}
		private const string TAG = "NetSceneTypingStatus";
		private const int CMD_CHATSTATUS = 111;
		private void onEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_DIRECTSEND_REQ)
			{
				byte[] array = evtArgs.mObject as byte[];
				if (array == null)
				{
					return;
				}
				NetSceneChatTyping.TypingStatusArgs typingStatusArgs = this.onParseTyping(array);
				if (typingStatusArgs == null)
				{
					return;
				}
				EventCenter.postEvent(EventConst.ON_NETSCENE_CHAT_TYPING, typingStatusArgs, null);
			}
		}
		public void doScene(string toUser, ChatTypingStatus status)
		{
			SessionPack sessionPack = new SessionPack();
			sessionPack.mCmdID = 8;
			NetSceneChatTyping.TypingStatusContext typingStatusContext = new NetSceneChatTyping.TypingStatusContext();
			typingStatusContext.byte_0 = 111;
			typingStatusContext.flag = 0;
			typingStatusContext.int_0 = (int)Util.getNowSeconds();
			typingStatusContext.receivers = toUser;
			typingStatusContext.content = new byte[4];
			int num = 0;
			Util.writeInt((int)status, ref typingStatusContext.content, ref num);
			sessionPack.mRequestObject = typingStatusContext;
			sessionPack.mProcRequestToByteArray += new RequestToByteArrayDelegate(this.requestToByteArray);
			Sender.getInstance().sendPack(sessionPack);
		}
		private byte[] requestToByteArray(object object_0)
		{
			NetSceneChatTyping.TypingStatusContext typingStatusContext = object_0 as NetSceneChatTyping.TypingStatusContext;
			if (typingStatusContext == null)
			{
				return null;
			}
			byte[] result;
			try
			{
				int num = 0;
				int num2 = 6 + typingStatusContext.receivers.get_Length() + 2 + typingStatusContext.content.Length;
				byte[] array = new byte[num2];
				Util.writeInt(typingStatusContext.int_0, ref array, ref num);
				Util.writeShort((short)typingStatusContext.receivers.get_Length(), ref array, ref num);
				Util.writeBuffer(Util.StringToByteArray(typingStatusContext.receivers), ref array, ref num);
				Util.writeShort((short)typingStatusContext.content.Length, ref array, ref num);
				Util.writeBuffer(typingStatusContext.content, ref array, ref num);
				byte[] hash = MD5Core.GetHash(Util.getDeviceUniqueId());
				byte[] array2 = DES.EncryptBytes(array, hash, 1);
				if (array2 == null)
				{
					result = null;
				}
				else
				{
					int num3 = 2 + array2.Length;
					byte[] array3 = new byte[num3];
					num = 0;
					Util.writeByte(typingStatusContext.byte_0, ref array3, ref num);
					Util.writeByte(typingStatusContext.flag, ref array3, ref num);
					Util.writeBuffer(array2, ref array3, ref num);
					result = array3;
				}
			}
			catch (Exception object_)
			{
				Log.smethod_1("NetSceneTypingStatus", object_);
				result = null;
			}
			return result;
		}
		private NetSceneChatTyping.TypingStatusArgs onParseTyping(byte[] data)
		{
			if (data != null && data.Length >= 10)
			{
				NetSceneChatTyping.TypingStatusArgs result;
				try
				{
					int num = 0;
					byte b = Util.readByte(data, ref num);
					if (b != 111)
					{
						result = null;
					}
					else
					{
						byte[] array = new byte[data.Length - 2];
						Buffer.BlockCopy(data, 2, array, 0, data.Length - 2);
						byte[] hash = MD5Core.GetHash(Util.getDeviceUniqueId());
						int num2 = array.Length + 32;
						byte[] array2 = new byte[num2];
						if (DES.Using_DES(array2, ref num2, array, array.Length, hash, hash.Length, 2) == 0)
						{
							result = null;
						}
						else
						{
							byte[] array3 = new byte[num2];
							num = 0;
							Array.Copy(array2, array3, num2);
							Util.readInt(array3, ref num);
							short num3 = Util.readShort(array3, ref num);
							byte[] byte_ = new byte[(int)num3];
							Util.readBuffer(array3, ref num, ref byte_, (int)num3);
							short num4 = Util.readShort(array3, ref num);
							if (num4 != 4)
							{
								result = null;
							}
							else
							{
								byte[] byte_2 = new byte[(int)num4];
								Util.readBuffer(array3, ref num, ref byte_2, (int)num4);
								NetSceneChatTyping.TypingStatusArgs typingStatusArgs = new NetSceneChatTyping.TypingStatusArgs();
								typingStatusArgs.fromUser = Util.ByteArrayToString(byte_);
								num = 0;
								typingStatusArgs.status = (ChatTypingStatus)Util.readInt(byte_2, ref num);
								result = typingStatusArgs;
							}
						}
					}
				}
				catch (Exception object_)
				{
					Log.smethod_1("NetSceneTypingStatus", object_);
					result = null;
				}
				return result;
			}
			return null;
		}
	}
}
